﻿using UnityEngine;
using QFramework;
using DG.Tweening;
using HKTools;
using UnityEngine.UI;

/// <summary>
/// Camera多目标切换管理工具，测试脚本
/// </summary>
public class Test_UseTargetCam : HKTargetCam_BaseController
{
    // 相机状态及设置
    [SerializeField] Text tips_Txt;
    [SerializeField] Toggle toggle_FreezeCam;
    [SerializeField] Button btn_QueryTarget;
    [SerializeField] Slider slider_TargetChangeTime;
    [SerializeField] Text txt_TargetChangeTime;
    [SerializeField] Button btn_Ensure;

    // 正常切换方式的按钮
    [SerializeField] Button btn_RotTarget01;
    [SerializeField] Button btn_RotTarget02;
    [SerializeField] Button btn_RotTarget03;
    [SerializeField] Button btn_RotTarget04;
    [SerializeField] Button btn_AreaTarget01;
    [SerializeField] Button btn_AreaTarget02;
    [SerializeField] Button btn_BirdViewTarget01;
    [SerializeField] Button btn_FollowTarget01;

    // 闪现切换方式的按钮
    [SerializeField] Button btn_RotTarget01_Flash;
    [SerializeField] Button btn_RotTarget02_Flash;
    [SerializeField] Button btn_RotTarget03_Flash;
    [SerializeField] Button btn_RotTarget04_Flash;
    [SerializeField] Button btn_AreaTarget01_Flash;
    [SerializeField] Button btn_AreaTarget02_Flash;
    [SerializeField] Button btn_BirdViewTarget01_Flash;
    [SerializeField] Button btn_FollowTarget01_Flash;

    // 测试用的移动目标
    [SerializeField] Transform testMoveNPC;

    void Start()
    {
        // 设置相机的移动速度和缩放速度
        this.SendCommand(new CMD_SetMoveAndZoomSpeed(1.5f, 3));

        // 设置相机的俯仰角度范围，从5度到85度
        this.SendCommand(new CMD_SetPitchAngle(5, 85));

        toggle_FreezeCam.onValueChanged.AddListener(isOn =>
        {
            if (isOn)
            {
                // 冻结相机
                this.SendCommand(new CMD_SetFreezeMode(true));
            }
            else
            {
                // 解冻相机
                this.SendCommand(new CMD_SetFreezeMode(false));
            }
        });

        btn_QueryTarget.onClick.AddListener(() =>
        {
            // 获取当前相机的目标类型
            HK_TargetEnum targetType = this.SendQuery(new Query_GetCurrentTarget());
            HKDebug.Log("当前目标为：" + targetType.ToString());
        });

        txt_TargetChangeTime.text = slider_TargetChangeTime.value.ToString("0.00" + " 秒");
        slider_TargetChangeTime.onValueChanged.AddListener(value =>
        {
            txt_TargetChangeTime.text = value.ToString("0.00" + " 秒");
        });

        // 点击按钮，设置目标切换时间
        btn_Ensure.onClick.AddListener(() =>
        {
            var targetChangeTime = slider_TargetChangeTime.value;
            this.SendCommand(new CMD_SetTargetChangeTime(targetChangeTime));
        });

        btn_RotTarget01.onClick.AddListener(() => this.SendCommand(new CMD_ChangeCamTarget(HK_TargetEnum.RotTarget_01)));

        btn_RotTarget02.onClick.AddListener(() => this.SendCommand(new CMD_ChangeCamTarget(HK_TargetEnum.RotTarget_02)));

        btn_RotTarget03.onClick.AddListener(() => this.SendCommand(new CMD_ChangeCamTarget(HK_TargetEnum.RotTarget_03)));

        btn_RotTarget04.onClick.AddListener(() => this.SendCommand(new CMD_ChangeCamTarget(HK_TargetEnum.RotTarget_04)));

        btn_AreaTarget01.onClick.AddListener(() => this.SendCommand(new CMD_ChangeCamTarget(HK_TargetEnum.AreaTarget_01)));

        btn_AreaTarget02.onClick.AddListener(() => this.SendCommand(new CMD_ChangeCamTarget(HK_TargetEnum.AreaTarget_02)));

        btn_BirdViewTarget01.onClick.AddListener(() => this.SendCommand(new CMD_ChangeCamTarget(HK_TargetEnum.BirdViewTarget_01)));

        btn_FollowTarget01.onClick.AddListener(() =>
        {
            this.SendCommand(new CMD_ChangeCamTarget(HK_TargetEnum.FollowTarget_01));

            /*---------------------测试-----------------*/
            testMoveNPC.position = new Vector3(-15f, 1, 4.5f);
            testMoveNPC.DOMoveX(13f, 6f).SetEase(Ease.Linear).SetDelay(1f);
            /*------------------------------------------*/
        });

        btn_RotTarget01_Flash.onClick.AddListener(() => this.SendCommand(new CMD_ChangeCamTarget(HK_TargetEnum.RotTarget_01).SetUsingFlash()));

        btn_RotTarget02_Flash.onClick.AddListener(() => this.SendCommand(new CMD_ChangeCamTarget(HK_TargetEnum.RotTarget_02).SetUsingFlash()));

        btn_RotTarget03_Flash.onClick.AddListener(() => this.SendCommand(new CMD_ChangeCamTarget(HK_TargetEnum.RotTarget_03).SetUsingFlash()));

        btn_RotTarget04_Flash.onClick.AddListener(() => this.SendCommand(new CMD_ChangeCamTarget(HK_TargetEnum.RotTarget_04).SetUsingFlash()));

        btn_AreaTarget01_Flash.onClick.AddListener(() => this.SendCommand(new CMD_ChangeCamTarget(HK_TargetEnum.AreaTarget_01).SetUsingFlash()));

        btn_AreaTarget02_Flash.onClick.AddListener(() => this.SendCommand(new CMD_ChangeCamTarget(HK_TargetEnum.AreaTarget_02).SetUsingFlash()));

        btn_BirdViewTarget01_Flash.onClick.AddListener(() => this.SendCommand(new CMD_ChangeCamTarget(HK_TargetEnum.BirdViewTarget_01).SetUsingFlash()));

        btn_FollowTarget01_Flash.onClick.AddListener(() =>
        {
            this.SendCommand(new CMD_ChangeCamTarget(HK_TargetEnum.FollowTarget_01).SetUsingFlash());

            /*---------------------测试-----------------*/
            testMoveNPC.position = new Vector3(-15f, 1, 4.5f);
            testMoveNPC.DOMoveX(13f, 6f).SetEase(Ease.Linear).SetDelay(1f);
            /*------------------------------------------*/
        });

        // 注册 开始切换 Target 事件
        this.RegisterEvent<Event_SwitchTargetBegin>(args =>
        {
            HKDebug.Log($"开始 切换 Target：{args._targetEnum}");
        }).UnRegisterWhenGameObjectDestroyed(gameObject);

        // 注册 完成切换 Target 事件
        this.RegisterEvent<Event_SwitchTargetEnd>(args =>
        {
            HKDebug.Log($"完成 切换 Target：{args._targetEnum}");
            tips_Txt.text = $"当前 Target 为：{args._targetEnum}";
        }).UnRegisterWhenGameObjectDestroyed(gameObject);
    }
}
